package com.xpxrule.strategy.field;

import com.xpxrule.dataobject.base.Range;

import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0
 * @Author xiangpengxiang
 * @Date 2024/12/25 10:35
 * @注释
 */
public abstract class FieldTypeStrategy<T> {

    public static final String RANGE_DISPLAY_TEXT_FORMAT = "（%s   %s）";
    public static final String COLLECTION_DISPLAY_TEXT_FORMAT = "（%s）";
    public T getFieldObject(Object singleValue) {
        // 将输入内容统一格式
        String convert = classConvertToString(singleValue);
        // 将统一格式转为目标类型
        return getFieldObjectSingleValue(convert);
    }

    /**
     * 获取变量参数
     * @param singleValue
     * @return
     */
    public T getEnvFieldParamObject(Object singleValue) {
        return getFieldObject(singleValue);
    }

    /**
     * 将统一格式字符串转换为目标类型
     * @param singleValue
     * @return
     */
    public T getFieldObjectSingleValue(String singleValue) {
        return stringConvertClass(singleValue);
    }

    public Range getFieldRange(Range rangValue) {
        T begin = getFieldObject(rangValue.getBegin());
        T end = getFieldObject(rangValue.getEnd());
        return new Range<T>(begin, end);
    }

    public List<T> getFieldCollection(List<Object> listValue) {
        List<T> collection = new ArrayList<>();
        for (Object value : listValue) {
            T fieldObject = getFieldObject(value);
            collection.add(fieldObject);
        }
        return collection;
    }

    public List<T> getEnvFieldCollection(List<Object> listValue) {
        List<T> collection = new ArrayList<>();
        for (Object value : listValue) {
            T fieldObject = getEnvFieldParamObject(value);
            collection.add(fieldObject);
        }
        return collection;
    }

    public String getSingleValueDisplayText(Object singleValue) {
        T fieldObject = getFieldObject(singleValue);
        return convertToDisplayText(fieldObject);
    }

    public String getRangeValueDisplayText(Range rangeValue) {
        Range fieldRange = getFieldRange(rangeValue);
        String begin = convertToDisplayText((T) fieldRange.getBegin());
        String end = convertToDisplayText((T) fieldRange.getEnd());
        return String.format(RANGE_DISPLAY_TEXT_FORMAT, begin, end);
    }

    public String getCollectionValueDisplayText(List<Object> listValue) {
        List<T> fieldCollection = getFieldCollection(listValue);
        List<String> displayTextList = new ArrayList<>();
        for (T fieldObject : fieldCollection) {
            displayTextList.add(convertToDisplayText(fieldObject));
        }
        String join = String.join(", ", displayTextList);
        return String.format(COLLECTION_DISPLAY_TEXT_FORMAT, join);
    }

    abstract String convertToDisplayText(T value);

    abstract String classConvertToString(Object value);

    abstract T stringConvertClass(String value);

    abstract T[] getFieldObjectByRangeValue(Range<String> rangeValue);

    abstract T getFieldObjectByListValue(List<String> listValue);
}
